package com.hy.main.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author yang DongYong
 * @Date 2022/5/12 - 05 - 12 - 0:41
 * @Description: com.hy.main.config
 * @version: 1.0
 * TTL 配置类代码
 */
@Configuration
public class TtlQueueConfig {
    // 普通交换机名称
    public static final String X_EXCHANGE = "X";
    // 死信交换机名称
    public static final String Y_DEAD_EXCHANGE = "Y";
    // 普通队列名称
    public static final String X_QUEUE_A = "QA";
    public static final String X_QUEUE_B = "QB";
    public static final String X_QUEUE_C = "QC";
    // 死信队列名称
    public static final String Y_DEAD_QUEUE = "QD";
    // 死信队列routingKey
    public static final String DEAD_ROUTING_KEY = "YD";

    // 声明xExchange普通交换机
    @Bean
    public DirectExchange xExchange() {
      return new DirectExchange(X_EXCHANGE);
    }

    // 声明yExchange死信交换机
    @Bean
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_EXCHANGE);
    }

    // 声明普通队列A
    @Bean
    public Queue queueA() {
        HashMap<String, Object> queueMap = new HashMap<>(3);
        // 设置死信交换机
        queueMap.put("x-dead-letter-exchange",Y_DEAD_EXCHANGE);
        // 设置死信队列RoutingKey
        queueMap.put("x-dead-letter-routing-key",DEAD_ROUTING_KEY);
        // 设置死信TTL存活时间 单位是ms
        queueMap.put("x-message-ttl",10000);
        return QueueBuilder.durable(X_QUEUE_A).withArguments(queueMap).build();
    }

    // 声明普通队列B
    @Bean
    public Queue queueB() {
        HashMap<String, Object> queueMap = new HashMap<>(3);
        // 设置死信交换机
        queueMap.put("x-dead-letter-exchange",Y_DEAD_EXCHANGE);
        // 设置死信队列RoutingKey
        queueMap.put("x-dead-letter-routing-key",DEAD_ROUTING_KEY);
        // 设置死信TTL存活时间 单位是ms
        queueMap.put("x-message-ttl",40000);
        return QueueBuilder.durable(X_QUEUE_B).withArguments(queueMap).build();
    }

    // 声明普通队列C
    @Bean
    public Queue queueC() {
        HashMap<String, Object> queueMap = new HashMap<>();
        // 设置死信交换机
        queueMap.put("x-dead-letter-exchange",Y_DEAD_EXCHANGE);
        // 设置死信队列RoutingKey
        queueMap.put("x-dead-letter-routing-key",DEAD_ROUTING_KEY);
        return QueueBuilder.durable(X_QUEUE_C).withArguments(queueMap).build();
    }

    // 死信队列
    @Bean
    public Queue queueD() {
        return  QueueBuilder.durable(Y_DEAD_QUEUE).build();
    }

    // 绑定队列A
    @Bean
    public Binding queueQaBindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    // 绑定队列B
    @Bean
    public Binding queueQbBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    // 绑定队列C
    @Bean
    public Binding queueQcBindingX(@Qualifier("queueC") Queue queueC,
                                   @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

    // 绑定队列D
    @Bean
    public Binding queueQdBindingX(@Qualifier("queueD") Queue queueD,
                                   @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }

}
